package com.example.qqfake;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

/*
* 消息，记录发送时间，内容，（发送客户端）
* sendMessage(Socket client,String goal)利用服务器，将消息发送到目标客户端
* */
public class Content {
    //type: 0 代表登录消息，1 代表注册消息， 2 代表普通消息， 3 代表添加好友， 4 代表修改用户信息 5 代表删除联系人 6 代表搜索用户 7 查找所有好友 8 代表在线信息
    //send_id 同上
    private String type;
    private String send_id;
    private String content;
    private String get_id;
    private String time;

    public Content(String type,String send_id, String time, String content, String get_id) {
        this.type=type;

        this.send_id=send_id;
        this.content=content;
        this.get_id=get_id;
        this.time=time;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSend_id() {
        return send_id;
    }

    public void setSend_id(String send_id) {
        this.send_id = send_id;
    }

    public String getGet_id() {
        return get_id;
    }

    public void setGet_id(String get_id) {
        this.get_id = get_id;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTime() {
        return time;
    }

    public void sendMessage(Socket client) throws IOException {
        //格式：发送类型，发送方id：发送时间：发送内容：接收方id
        String cc = type+","+send_id+","+time+","+content+","+get_id+"\r";
//        OutputStream os =client.getOutputStream();
//        os.write(cc.getBytes());

        PrintStream printStream = new PrintStream(client.getOutputStream());
        //向该输出流中写入要转发的内容
        printStream.println(cc);

    }
    public Content getMessage(Socket client) throws IOException {
        //获取输入对象
        InputStream is = client.getInputStream();
        //创建字节数组
        byte[] b = new byte[1024];
        //读取字符数组
        int len = is.read(b);
        //解析字符数组
        String msg = new String(b,0,len);
        String[] parts = msg.split(",");
        Content message = new Content(parts[0],parts[1],parts[2],parts[3],parts[4]);


        return message;

    }


}
